package com.xps.charmingshop.view.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.xps.charmingshop.R;
import com.xps.charmingshop.utils.SharedPreferencesUtils;
import com.xps.charmingshop.view.base.BaseActivity;
import com.xps.charmingshop.view.fragment.BillFragment;
import com.xps.charmingshop.view.fragment.CompleteFragment;
import com.xps.charmingshop.view.fragment.ConductFragment;
import com.xps.charmingshop.view.fragment.ListFragment;
import com.xps.charmingshop.view.fragment.ShopFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 吾丶说谎者 on 2017/11/21.
 */

public class MainActivity extends BaseActivity {


    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.main_container)
    FrameLayout mainContainer;
    @BindView(R.id.tv_main)
    TextView tvMain;
    @BindView(R.id.tv_dynamic)
    TextView tvDynamic;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    ListFragment listFragment;
    CompleteFragment completeFragment;
    ConductFragment conductFragment;
    BillFragment billFragment;
    ShopFragment shopFragment;
    @BindView(R.id.tv_shop)
    TextView tvShop;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void setData() {
        actionbarIvBack.setVisibility(View.GONE);
        actionbarTvTitle.setText("首页");
        tvMain.setSelected(true);
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        listFragment = new ListFragment();
        completeFragment = new CompleteFragment();
        conductFragment = new ConductFragment();
        billFragment = new BillFragment();
        shopFragment = new ShopFragment();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, listFragment).disallowAddToBackStack().commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, completeFragment).disallowAddToBackStack().show(completeFragment).commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, conductFragment).disallowAddToBackStack().show(conductFragment).commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, billFragment).disallowAddToBackStack().show(billFragment).commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, shopFragment).disallowAddToBackStack().show(shopFragment).commit();
        completeFragment.hide();
        conductFragment.hide();
        billFragment.hide();
        shopFragment.hide();
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        Log.i("asdasdasdsadasdasasd", "" + user_token);

//        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//            return;
//        }
//        //允许GPS、WiFi、基站定位，设置超时时间5秒
//        TrackerSettings trackerSettings = new TrackerSettings();
//        trackerSettings.setUseGPS(true).setUseNetwork(true).setUsePassive(true).setTimeout(5000);
//        LocationTracker locationTracker = new LocationTracker(this, trackerSettings) {
//            public void onLocationFound(Location location) {
//                //定位成功时回调
//                if (location != null) {
//                    Log.i(TAG, "经纬度：" + location.getLongitude() + "," + location.getLatitude());
//                }
//
//            }
//
//            public void onTimeout() {
//                //定位超时回调
//                Log.i(TAG, "定位超时");
//            }
//        };
//        locationTracker.startListening();
//    }

}


    @OnClick({R.id.tv_main, R.id.tv_dynamic, R.id.tv_message, R.id.tv_person, R.id.tv_shop})
    public void onViewClicked(View view) {
        tvMain.setSelected(false);
        tvDynamic.setSelected(false);
        tvMessage.setSelected(false);
        tvPerson.setSelected(false);
        tvShop.setSelected(false);
        switch (view.getId()) {
            case R.id.tv_main:
                tvMain.setSelected(true);
                actionbarTvTitle.setText("首页");
                showFragment(listFragment);
                break;
            case R.id.tv_dynamic:
                tvDynamic.setSelected(true);
                actionbarTvTitle.setText("我的订单");
                showFragment(completeFragment);
                break;
            case R.id.tv_message:
                tvMessage.setSelected(true);
                actionbarTvTitle.setText("订单列表");
                showFragment(conductFragment);
                break;
            case R.id.tv_person:
                tvPerson.setSelected(true);
                actionbarTvTitle.setText("我的账单");
                showFragment(billFragment);
                break;
            case R.id.tv_shop:
                tvShop.setSelected(true);
                actionbarTvTitle.setText("店铺设置");
                showFragment(shopFragment);
                break;
        }
    }

    public void showFragment(Fragment showFragemt) {
        hideFrags();
        this.getSupportFragmentManager().beginTransaction().disallowAddToBackStack().show(showFragemt).commit();
    }

    public void hideFrags() {
        listFragment.hide();
        completeFragment.hide();
        conductFragment.hide();
        billFragment.hide();
        shopFragment.hide();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}